package com.mbs.project.mbs.dispose.controller;

import java.util.List;

import com.mbs.common.utils.DateUtils;
import com.mbs.common.utils.poi.ExcelUtil;
import com.mbs.framework.aspectj.lang.annotation.Log;
import com.mbs.framework.aspectj.lang.enums.BusinessType;
import com.mbs.framework.web.controller.BaseController;
import com.mbs.framework.web.domain.AjaxResult;
import com.mbs.framework.web.page.TableDataInfo;
import com.mbs.project.system.user.domain.User;
import com.mbs.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.mbs.project.mbs.dispose.domain.MessageBoardDispose;
import com.mbs.project.mbs.dispose.service.IMessageBoardDisposeService;

/**
 * 留言审核Controller
 *
 * @author xyl
 * @date 2022-02-03
 */
@Controller
@RequestMapping("/mbs/dispose")
public class MessageBoardDisposeController extends BaseController {
    private String prefix = "mbs/dispose";

    @Autowired
    private IMessageBoardDisposeService messageBoardDisposeService;
    @Autowired
    private IUserService userService;

    @RequiresPermissions("mbs:dispose:view")
    @GetMapping()
    public String dispose() {
        return prefix + "/dispose";
    }

    /**
     * 查询留言审核列表
     */
    @RequiresPermissions("mbs:dispose:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MessageBoardDispose messageBoardDispose) {
        startPage();
        List<MessageBoardDispose> list = messageBoardDisposeService.selectMessageBoardDisposeList(messageBoardDispose);
        return getDataTable(list);
    }

    /**
     * 导出留言审核列表
     */
    @RequiresPermissions("mbs:dispose:export")
    @Log(title = "留言审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MessageBoardDispose messageBoardDispose) {
        List<MessageBoardDispose> list = messageBoardDisposeService.selectMessageBoardDisposeList(messageBoardDispose);
        ExcelUtil<MessageBoardDispose> util = new ExcelUtil<MessageBoardDispose>(MessageBoardDispose.class);
        return util.exportExcel(list, "dispose");
    }

    /**
     * 新增留言审核
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @RequiresPermissions("mbs:dispose:content:status")
    @GetMapping("/reply")
    public String reply(@RequestParam(name = "mbId") Long mbId,ModelMap mmap) {
        mmap.put("mbId",mbId);
        return prefix + "/reply";
    }

    @RequiresPermissions("mbs:dispose:content:status")
    @Log(title = "回复消息", businessType = BusinessType.UPDATE)
    @PostMapping("/reply")
    @ResponseBody
    public AjaxResult replyContent(MessageBoardDispose messageBoardDispose) {
        return toAjax(messageBoardDisposeService.updateContentByMbId(messageBoardDispose));
    }


    /**
     * 分配等级页面
     * @param mbIds
     * @return
     */
    @GetMapping("/distribute/level")
    public String distributeLevel(@RequestParam("mbIds") String mbIds, ModelMap mmap) {
        mmap.put("mbIds",mbIds);
        return prefix + "/level";
    }

    @RequiresPermissions("mbs:dispose:distribute:level")
    @Log(title = "分配等级", businessType = BusinessType.UPDATE)
    @PostMapping("/distribute/level")
    @ResponseBody
    public AjaxResult distributeLevelSave(MessageBoardDispose messageBoardDispose) {
        return toAjax(messageBoardDisposeService.updateLevelBatch(messageBoardDispose));
    }

    /**
     * 分配业务员页面
     * @param mbIds
     * @return
     */
    @GetMapping("/distribute/verifier")
    public String distributeVerifier(@RequestParam("mbIds") String mbIds, ModelMap mmap) {
        //查询业务员
        List<User> userListByRoleId = userService.getUserListByRoleId(2);
        mmap.put("disposer",userListByRoleId);
        mmap.put("mbIds",mbIds);
        return prefix + "/verifier";
    }

    @RequiresPermissions("mbs:dispose:distribute:verifier")
    @Log(title = "分配业务员", businessType = BusinessType.INSERT)
    @PostMapping("/distribute/verifier")
    @ResponseBody
    public AjaxResult distributeVerifierSave(MessageBoardDispose messageBoardDispose) {
        return toAjax(messageBoardDisposeService.updateVerifierBatch(messageBoardDispose));
    }

    /**
     * 新增保存留言审核
     */
    @RequiresPermissions("mbs:dispose:add")
    @Log(title = "留言审核", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MessageBoardDispose messageBoardDispose) {
        return toAjax(messageBoardDisposeService.insertMessageBoardDispose(messageBoardDispose));
    }

    /**
     * 修改留言审核
     */
    @GetMapping("/edit/{mbDisposeId}")
    public String edit(@PathVariable("mbDisposeId") Long mbDisposeId, ModelMap mmap) {
        MessageBoardDispose messageBoardDispose = messageBoardDisposeService.selectMessageBoardDisposeById(mbDisposeId);
        mmap.put("messageBoardDispose", messageBoardDispose);
        return prefix + "/edit";
    }

    /**
     * 修改保存留言审核
     */
    @RequiresPermissions("mbs:dispose:edit")
    @Log(title = "留言审核", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MessageBoardDispose messageBoardDispose) {
        return toAjax(messageBoardDisposeService.updateMessageBoardDispose(messageBoardDispose));
    }
    /**
     * 修改保存留言审核
     */
    @RequiresPermissions("mbs:dispose:status")
    @Log(title = "留言状态", businessType = BusinessType.UPDATE)
    @PostMapping("/status")
    @ResponseBody
    public AjaxResult editStatusSave(MessageBoardDispose messageBoardDispose) {
        messageBoardDispose.setUpdateTime(DateUtils.getNowDate());
        messageBoardDispose.setUpdateBy(getLoginName());
        return toAjax(messageBoardDisposeService.updateStatusByMbId(messageBoardDispose));
    }

    /**
     * 删除留言审核
     */
    @RequiresPermissions("mbs:dispose:remove")
    @Log(title = "留言审核", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(messageBoardDisposeService.deleteMessageBoardDisposeByIds(ids));
    }
}
